/**
 * Created by Administrator on 2015/12/25.
 */

//初始化模块，依赖ngRoute模块
var app = angular.module("app",['ngRoute','ui.bootstrap']);

//服务
app.factory("apiUrl",function(){
    var data = {
        "write":"server/write.php",
        "show":"server/show.php",
        "info":"server/info.php"
    };
    return data;
})


//路由
app.config(function($routeProvider){
   $routeProvider.when('/contents',{
       controller: 'contentsCtrl',
       templateUrl: 'contents.html',
       publicAccess:true
   }).when('/form',{
       controller: 'formCtrl',
       templateUrl: 'form.html'
   }).when('/direc',{
       controller: 'direcCtrl',
       templateUrl: 'direc.html'
   }).when('/message',{
       controller: 'messageCtrl',
       templateUrl: 'message.html'
   }).when('/show',{
       controller: 'showCtrl',
       templateUrl: 'show.html'
   }).when('/info/:id/:phone',{
       controller: 'infoCtrl',
       templateUrl: 'info.html'
   }).otherwise({
       templateUrl: 'error.html',
   })
});


//控制器
app.controller('contentsCtrl',function($scope,$http){
    $scope.params = {
        'name':'kang',
        'title':"this is contents"
    }
}).controller('formCtrl',['$scope', '$http',
    function ($scope, $http){
        $scope.params = {
            'title':"1"
        }
        $scope.model = {
            'id':10,
            'name':'kang',
            'email':'94343@qq.com'
        }
        $scope.requireValue = true;
        $scope.queryCache = {};
        $scope.index = 0;
        $scope.add= function()
        {
            $http.post("server.php").success(function (data) {
                $scope.queryCache[$scope.index] = data;
                $scope.index = $scope.index + 1 ;
            })

        }
    }
]).controller('direcCtrl',function($scope,remoteData){
    $scope.params = {
        'title':"this is directives"
    }

    $scope.name = remoteData.name;
}).controller('messageCtrl',function($scope,$http,apiUrl){
    $scope.params = {
        'title':'Message Write'
    }
    //提交到服务端
    $scope.toSubmit=function()
    {
        var postData = {
            "name":$scope.value.name,
            "email":$scope.value.email,
            "phone":$scope.value.phone,
            "message":$scope.value.message
        }
        var req = {
            method: "post",
            url   : apiUrl.write,
            data  : postData
        }

        $http(req).then(
            function(data){
                console.log(data)
            }
        )
    }

}).controller('showCtrl',function($scope, $http, apiUrl){
    $scope.params = {
        "title":"Message Show"
    }

    $scope.currentPage = 1;

    $scope.pagedata = function(){
        console.log($scope.currentPage)
        var req = {
            method:"post",
            url: apiUrl.show,
            data:{"currentPage":$scope.currentPage}
        }
        $http(req).then(
            function(response){
                $scope.message = response.data
                $scope.total = 50;
                $scope.size = 10;
            }
        )
    }

    $scope.$watch("currentPage",function(newValue, oldValue, scope){
        $scope.pagedata();
    });
}).controller('infoCtrl',function($scope, $routeParams, $http, apiUrl){
    $scope.title = "info";
    $scope.id = $routeParams.id;
    $scope.phone = $routeParams.phone;

})






//过滤器
app.filter('dateDiy',function(){
    return function (input){
        return input*1000;
    }
})

app.filter('messageSubstr',function(){
    return function (input){
        return input.slice(0,5) + "...";
    }
})




//指令
app.directive('firstdirec',function(){
    return {
        restrict:"AECM",
        template:"hello,world",
        compile:function(tElem,attrs){
            return function(){
                tElem.html('click me')
            }
        },
        link: function ($scope, element, attrs) {
            element.bind('click',function(){
                this.style.background="red";
            });
        }

    }
})

app.directive("page",function(){
    return {
        restrict:"E",
        templateUrl:"page.html",
        replace:true,
        scope:{
            total:'=',
            size:'=',
            current:'='
        },
        link:function(scope){
            scope.$watch("total", function(){
                var pageNum = scope.total / scope.size;
                scope.pageList= [];
                for(var i=1;i<=pageNum;i++)
                {
                    scope.pageList.push(i)
                }
            });

            scope.changePage = function(val)
            {
                if(val == 'pre'){
                    scope.current--
                }else if(val == 'next'){
                    scope.current++
                }else{
                    scope.current = val
                }
            }
        }
    }

})




















































